package com.landscope.issue.service.applet;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.landscope.issue.bean.E_ALARMVAL_LEVEL;
import com.landscope.issue.bean.E_TOP_PARAM_NAME_TYPE;
import com.landscope.issue.bean.E_TOP_PARAM_TYPE;
import com.landscope.issue.controller.api.dto.CoreDataDto;
import com.landscope.issue.controller.api.dto.CoreMonitorDataDto;
import com.landscope.issue.controller.api.dto.MonitorDataDto;
import com.landscope.issue.controller.base.ResponseResult;
import com.landscope.issue.controller.wx.dto.AppletMainMessageDto;
import com.landscope.issue.controller.wx.dto.AppletMonitorDataDto;
import com.landscope.issue.controller.wx.dto.AppletMonitorViewDto;
import com.landscope.issue.controller.wx.param.AppletMainGetNoticeParam;
import com.landscope.issue.controller.wx.param.AppletMonitorAddTopParam;
import com.landscope.issue.controller.wx.param.AppletMonitorDeleteTopParam;
import com.landscope.issue.controller.wx.param.AppletMonitorViewParam;
import com.landscope.issue.entity.Monitordata;
import com.landscope.issue.entity.Project;
import com.landscope.issue.entity.TopParam;
import com.landscope.issue.entity.User;
import com.landscope.issue.mapper.MonitordataMapper;
import com.landscope.issue.mapper.ProjectMapper;
import com.landscope.issue.mapper.TopParamMapper;
import com.landscope.issue.service.ApiUserService;
import com.landscope.issue.utils.CommonUtil;
import com.landscope.issue.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ApiAppletMonitorService {

    @Resource
    private TopParamMapper topParamMapper;
    @Resource
    private MonitordataMapper monitordataMapper;
    @Resource
    private ProjectMapper projectMapper;

    public ResponseResult<AppletMonitorDataDto> data(AppletMainGetNoticeParam param) {
        // 验证token
        TokenUtil.verifyToken();
        // 验证项目权限
        User user = TokenUtil.verifyAuthority(param.getAccessToken(), param.getProjectCode());

        Monitordata monitordata = monitordataMapper.findLastByCode(param.getProjectCode());
        // 获取一周内参数
        Calendar calendar = Calendar.getInstance();
        String endDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_WEEK, -7);
        String startDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
        List<Monitordata> monitorDataWeekList = monitordataMapper.findByCode(param.getProjectCode(), startDate, endDate);

        // 获取项目
        Project project = projectMapper.findByCode(param.getProjectCode());

        String nameSta = project.getProjectNamesta().toUpperCase();
        // 将台站号替换为台站别名
        Map<String, String> nameStaMap = new HashMap<>();
        if (StrUtil.isNotEmpty(nameSta)) {
            String[] strs = nameSta.split(";");
            for (String key : strs) {
                String[] ss = key.split("-");
                nameStaMap.put(ss[0], ss[1]);
            }
        }

        // 获取置顶参数
        List<TopParam> topParamsItemList = topParamMapper.findByAccountAndProjectCode(user.getAccount(), param.getProjectCode());
        List<AppletMonitorDataDto.AppletMonitorDataTopParamsItem> topParamsItemDtoList = new ArrayList<>();
        for (TopParam item :
                topParamsItemList) {
            AppletMonitorDataDto.AppletMonitorDataTopParamsItem paramsItem = new AppletMonitorDataDto.AppletMonitorDataTopParamsItem();

            DateTime monitorDataDate = DateUtil.parse(monitordata.getDate().replaceAll("T", " "));
            paramsItem.setDate(DateUtil.format(monitorDataDate, "MM月dd日"));
            paramsItem.setId(item.getId().toString());
            paramsItem.setDes(CommonUtil.friendlyTime(monitorDataDate) + "更新");
            paramsItem.setLevel(E_ALARMVAL_LEVEL.valueOf(monitordata.getAlarmval()).getValue());
            paramsItem.setType(item.getType());
            paramsItem.setParamName(item.getParamName());

            paramsItem.setStaName(item.getParamName());


//            if (item.getType() == E_TOP_PARAM_TYPE.微裂隙变化均值.getValue() ||
//                    item.getType() == E_TOP_PARAM_TYPE.波速变化均值.getValue()) {
//                // 查找台站对名称
//                String[] paramNames = item.getParamName().split("-");
//                StringBuffer stringBuffer = new StringBuffer();
//                for (String s :
//                        paramNames) {
//                    stringBuffer.append(nameStaMap.get(s));
//                    stringBuffer.append("-");
//                }
//                String staName = stringBuffer.toString();
//                if (staName.endsWith("-"))
//                    staName.substring(0, staName.length() - 1);
//                paramsItem.setStaName(staName);
//            }

            if (item.getType() == E_TOP_PARAM_TYPE.微裂隙变化均值.getValue()) {
                paramsItem.setTitle(E_TOP_PARAM_TYPE.微裂隙变化均值.getName());

                // 遍历数据，kdig
                String kdig = monitordata.getKdig();
                String[] kdigs = kdig.split(";");
                for (String key : kdigs) {
                    //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                    String[] vs = key.split(":");
                    String name = paramsItem.getStaName();
                    String[] names = name.split(":");
                    String[] v1s = vs[0].split("_");
                    String[] v2s = names[0].split("-");
                    if (v2s.length == 2 && (
                            (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1]))
                    )) {
                        if (vs[1].compareTo("100") > 0)
                            paramsItem.setValue("N/A");
                        else
                            paramsItem.setValue(vs[1]);
                        break;
                    } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                        if (vs[1].compareTo("100") > 0)
                            paramsItem.setValue("N/A");
                        else
                            paramsItem.setValue(vs[1]);
                        break;
                    }
                }

                // 获取一周内最值
//                Map<String,String> maxMap = new HashMap<>();
//                Map<String,String> minMap = new HashMap<>();
                for (Monitordata weekItem :
                        monitorDataWeekList) {
                    kdig = weekItem.getKdig();
                    kdigs = kdig.split(";");
                    for (String key : kdigs) {
                        //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                        String[] vs = key.split(":");
                        String name = paramsItem.getStaName();
                        String[] names = name.split(":");
                        String[] v1s = vs[0].split("_");
                        String[] v2s = names[0].split("-");
                        if ((v2s.length == 2 && (
                                (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1])))) ||
                                (v2s.length == 1 && v2s[0].contains(v1s[0]))
                        ) {
                            if (vs[1].compareTo("100") < 0) {
                                if (StrUtil.isEmpty(paramsItem.getMax())) {
                                    paramsItem.setMax(vs[1]);
                                } else if (Float.valueOf(paramsItem.getMax()) < Float.valueOf(vs[1])) {
                                    paramsItem.setMax(vs[1]);
                                }
                                if (StrUtil.isEmpty(paramsItem.getMin())) {
                                    paramsItem.setMin(vs[1]);
                                } else if (Float.valueOf(paramsItem.getMin()) > Float.valueOf(vs[1])) {
                                    paramsItem.setMin(vs[1]);
                                }
                            }
                            break;
                        }
                    }
                }
            } else if (item.getType() == E_TOP_PARAM_TYPE.波速变化均值.getValue()) {
                paramsItem.setTitle(E_TOP_PARAM_TYPE.波速变化均值.getName());

                // 遍历数据，解析vvig数据
                String vvig = monitordata.getVvig();
                String[] vvigs = vvig.split(";");
                for (String key : vvigs) {
                    //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                    String[] vs = key.split(":");
                    String name = paramsItem.getStaName();
                    String[] names = name.split(":");
                    String[] v1s = vs[0].split("_");
                    String[] v2s = names[0].split("-");
                    if (v2s.length == 2 && (
                            (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1]))
                    )) {
                        if (vs[1].compareTo("100") > 0)
                            paramsItem.setValue("N/A");
                        else
                            paramsItem.setValue(vs[1]);
                        break;
                    } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                        if (vs[1].compareTo("100") > 0)
                            paramsItem.setValue("N/A");
                        else
                            paramsItem.setValue(vs[1]);
                        break;
                    }
                }

                // 获取一周内最值
                for (Monitordata weekItem :
                        monitorDataWeekList) {
                    vvig = weekItem.getKdig();
                    vvigs = vvig.split(";");
                    for (String key : vvigs) {
                        //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                        String[] vs = key.split(":");
                        String name = paramsItem.getStaName();
                        String[] names = name.split(":");
                        String[] v1s = vs[0].split("_");
                        String[] v2s = names[0].split("-");
                        if ((v2s.length == 2 && (
                                (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1])))) ||
                                (v2s.length == 1 && v2s[0].contains(v1s[0]))
                        ) {
                            if (vs[1].compareTo("100") < 0) {
                                if (StrUtil.isEmpty(paramsItem.getMax())) {
                                    paramsItem.setMax(vs[1]);
                                } else if (Float.valueOf(paramsItem.getMax()) < Float.valueOf(vs[1])) {
                                    paramsItem.setMax(vs[1]);
                                }
                                if (StrUtil.isEmpty(paramsItem.getMin())) {
                                    paramsItem.setMin(vs[1]);
                                } else if (Float.valueOf(paramsItem.getMin()) > Float.valueOf(vs[1])) {
                                    paramsItem.setMin(vs[1]);
                                }
                            }
                            break;
                        }
                    }
                }
            } else if (item.getType() == E_TOP_PARAM_TYPE.环境变量.getValue()) {
                paramsItem.setTitle(E_TOP_PARAM_NAME_TYPE.newValueOf(item.getParamName()).getName());

                String overalldd = monitordata.getOveralldd();
                String[] overalldds = overalldd.split(";");
                for (String key : overalldds) {
                    String[] vs = key.split(":");
                    if (item.getParamName().equals(vs[0])) {
                        paramsItem.setValue(vs[1]);
                        break;
                    }
                    //Rain:2.2;temperatue:5.17;PGV:2.5;Pressure:91.27;humity:68.05;lamination:58318;PGA:5.1;
                }

                // 获取一周内最值
                for (Monitordata weekItem :
                        monitorDataWeekList) {
                    overalldd = weekItem.getOveralldd();
                    overalldds = overalldd.split(";");
                    for (String key : overalldds) {
                        String[] vs = key.split(":");
                        if (item.getParamName().equals(vs[0])) {
                            if (StrUtil.isEmpty(paramsItem.getMax())) {
                                paramsItem.setMax(vs[1]);
                            } else if (Float.valueOf(paramsItem.getMax()) < Float.valueOf(vs[1])) {
                                paramsItem.setMax(vs[1]);
                            }
                            if (StrUtil.isEmpty(paramsItem.getMin())) {
                                paramsItem.setMin(vs[1]);
                            } else if (Float.valueOf(paramsItem.getMin()) > Float.valueOf(vs[1])) {
                                paramsItem.setMin(vs[1]);
                            }
                            break;
                        }
                    }
                }
            }
            topParamsItemDtoList.add(paramsItem);
        }

        // 获取波速变换参数
        List<AppletMonitorDataDto.AppletMonitorDataVvigListItem> vvigListItemList = new ArrayList<>();
        //  对比数据中台站对别名和 本次查找的台站对别名是否对应
        String[] strs = project.getProjectGstacal().split(";");
        for (String key : strs) {
            AppletMonitorDataDto.AppletMonitorDataVvigListItem vvigItem = new AppletMonitorDataDto.AppletMonitorDataVvigListItem();
            vvigItem.setTitle("波速变化均值");
            DateTime monitorDataDate = DateUtil.parse(monitordata.getDate().replaceAll("T", " "));
            vvigItem.setDate(DateUtil.format(monitorDataDate, "MM月dd日"));
            vvigItem.setDes(CommonUtil.friendlyTime(monitorDataDate) + "更新");
            vvigItem.setLevel(E_ALARMVAL_LEVEL.valueOf(monitordata.getAlarmval()).getValue());

            String[] s1 = key.split(":");
            if (s1.length == 2) {
                if (s1[0].equals("ACHL"))
                    continue;
                String[] s2 = s1[0].split("-");
                if (s2.length == 2) {
                    // 查找台站对名称
                    StringBuffer stringBuffer = new StringBuffer();
                    for (String s :
                            s2) {
                        stringBuffer.append(nameStaMap.get(s));
                        stringBuffer.append("-");
                    }
                    String staName = stringBuffer.toString();
                    if (staName.endsWith("-"))
                        staName.substring(0, staName.length() - 1);
                    vvigItem.setStaName(staName);
                } else if (s2.length == 1) {
                    vvigItem.setStaName(nameStaMap.get(s2[0]));
                }
            }

            String vvig = monitordata.getVvig();
            String[] vvigs = vvig.split(";");
            for (String kv : vvigs) {
                //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                String[] vs = kv.split(":");
                String name = vvigItem.getStaName();
                String[] names = name.split(":");
                String[] v1s = vs[0].split("_");
                String[] v2s = names[0].split("-");
                if (v2s.length == 2 && (
                        (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1]))
                )) {
                    if (vs[1].compareTo("100") > 0)
                        vvigItem.setValue("N/A");
                    else
                        vvigItem.setValue(vs[1]);
                    break;
                } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                    if (vs[1].compareTo("100") > 0)
                        vvigItem.setValue("N/A");
                    else
                        vvigItem.setValue(vs[1]);
                    break;
                }
            }

            // 获取一周内最值
            for (Monitordata weekItem :
                    monitorDataWeekList) {
                vvig = weekItem.getVvig();
                vvigs = vvig.split(";");
                for (String kv : vvigs) {
                    //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                    String[] vs = kv.split(":");
                    String name = vvigItem.getStaName();
                    String[] names = name.split(":");
                    String[] v1s = vs[0].split("_");
                    String[] v2s = names[0].split("-");
                    if ((v2s.length == 2 && (
                            (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1])))) ||
                            (v2s.length == 1 && v2s[0].contains(v1s[0]))
                    ) {
                        if (vs[1].compareTo("100") < 0) {
                            if (StrUtil.isEmpty(vvigItem.getMax())) {
                                vvigItem.setMax(vs[1]);
                            } else if (Float.valueOf(vvigItem.getMax()) < Float.valueOf(vs[1])) {
                                vvigItem.setMax(vs[1]);
                            }
                            if (StrUtil.isEmpty(vvigItem.getMin())) {
                                vvigItem.setMin(vs[1]);
                            } else if (Float.valueOf(vvigItem.getMin()) > Float.valueOf(vs[1])) {
                                vvigItem.setMin(vs[1]);
                            }
                        }
                        break;
                    }
                }
            }
            vvigListItemList.add(vvigItem);
        }

        // 获取微裂隙参数
        List<AppletMonitorDataDto.AppletMonitorDataKdigListItem> kdigListItemList = new ArrayList<>();
        //  对比数据中台站对别名和 本次查找的台站对别名是否对应
        String[] kgigStrs = project.getProjectGstacal().split(";");
        for (String key : kgigStrs) {
            AppletMonitorDataDto.AppletMonitorDataKdigListItem kdigItem = new AppletMonitorDataDto.AppletMonitorDataKdigListItem();

            kdigItem.setTitle("微裂隙变化均值");
            DateTime monitorDataDate = DateUtil.parseDateTime(monitordata.getDate().replaceAll("T", " "));
            kdigItem.setDate(DateUtil.format(monitorDataDate, "MM月dd日"));
            kdigItem.setDes(CommonUtil.friendlyTime(monitorDataDate) + "更新");
            kdigItem.setLevel(E_ALARMVAL_LEVEL.valueOf(monitordata.getAlarmval()).getValue());

            String[] s1 = key.split(":");
            if (s1.length == 2) {
                if (s1[0].equals("ACHL"))
                    continue;
                String[] s2 = s1[0].split("-");
                if (s2.length == 2) {
                    // 查找台站对名称
                    StringBuffer stringBuffer = new StringBuffer();
                    for (String s :
                            s2) {
                        stringBuffer.append(nameStaMap.get(s));
                        stringBuffer.append("-");
                    }
                    String staName = stringBuffer.toString();
                    if (staName.endsWith("-"))
                        staName.substring(0, staName.length() - 1);
                    kdigItem.setStaName(staName);
                } else if (s2.length == 1) {
                    kdigItem.setStaName(nameStaMap.get(s2[0]));
                }
            }

            String kdig = monitordata.getKdig();
            String[] vvigs = kdig.split(";");
            for (String kv : vvigs) {
                //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                String[] vs = kv.split(":");
                String name = kdigItem.getStaName();
                String[] names = name.split(":");
                String[] v1s = vs[0].split("_");
                String[] v2s = names[0].split("-");
                if (v2s.length == 2 && (
                        (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1]))
                )) {
                    if (vs[1].compareTo("100") > 0)
                        kdigItem.setValue("N/A");
                    else
                        kdigItem.setValue(vs[1]);
                    break;
                } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                    if (vs[1].compareTo("100") > 0)
                        kdigItem.setValue("N/A");
                    else
                        kdigItem.setValue(vs[1]);
                    break;
                }
            }

            // 获取一周内最值
            for (Monitordata weekItem :
                    monitorDataWeekList) {
                kdig = weekItem.getKdig();
                vvigs = kdig.split(";");
                for (String kv : vvigs) {
                    //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                    String[] vs = kv.split(":");
                    String name = kdigItem.getStaName();
                    String[] names = name.split(":");
                    String[] v1s = vs[0].split("_");
                    String[] v2s = names[0].split("-");
                    if ((v2s.length == 2 && (
                            (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1])))) ||
                            (v2s.length == 1 && v2s[0].contains(v1s[0]))
                    ) {
                        if (vs[1].compareTo("100") < 0) {
                            if (StrUtil.isEmpty(kdigItem.getMax())) {
                                kdigItem.setMax(vs[1]);
                            } else if (Float.valueOf(kdigItem.getMax()) < Float.valueOf(vs[1])) {
                                kdigItem.setMax(vs[1]);
                            }
                            if (StrUtil.isEmpty(kdigItem.getMin())) {
                                kdigItem.setMin(vs[1]);
                            } else if (Float.valueOf(kdigItem.getMin()) > Float.valueOf(vs[1])) {
                                kdigItem.setMin(vs[1]);
                            }
                        }
                        break;
                    }
                }
            }

            kdigListItemList.add(kdigItem);
        }

        // 获取环境变量参数,Rain:0.050;temperatue:31.783;PGV:1677.8397;Pressure:86.633;humity:42.063;lamination:35886;
        List<AppletMonitorDataDto.AppletMonitorDataEnviromentalListItem> enviromentalListItemList = new ArrayList<>();
        String overalldd = monitordata.getOveralldd();
        String[] overalldds = overalldd.split(";");
        for (String key : overalldds) {
            String[] vs = key.split(":");
            E_TOP_PARAM_NAME_TYPE e_top_param_name_type = E_TOP_PARAM_NAME_TYPE.newValueOf(vs[0]);
            if (e_top_param_name_type != null) {
                AppletMonitorDataDto.AppletMonitorDataEnviromentalListItem enviromentalItem = new AppletMonitorDataDto.AppletMonitorDataEnviromentalListItem();
                enviromentalListItemList.add(enviromentalItem);

                enviromentalItem.setTitle(e_top_param_name_type.getName());
                DateTime monitorDataDate = DateUtil.parse(monitordata.getDate().replaceAll("T", " "));
                enviromentalItem.setDate(DateUtil.format(monitorDataDate, "MM月dd日"));
                enviromentalItem.setDes(CommonUtil.friendlyTime(monitorDataDate) + "更新");
                enviromentalItem.setValue(vs[1]);
                enviromentalItem.setLevel(E_ALARMVAL_LEVEL.valueOf(monitordata.getAlarmval()).getValue());

                // 获取一周内最值
                for (Monitordata weekItem :
                        monitorDataWeekList) {
                    overalldd = weekItem.getOveralldd();
                    overalldds = overalldd.split(";");
                    for (int i = 0; i < overalldds.length; i++) {
                        String kv = overalldds[i];
                        String[] vs2 = kv.split(":");
                        if (vs[0].equals(vs2[0])) {
                            if (StrUtil.isEmpty(enviromentalItem.getMax())) {
                                enviromentalItem.setMax(vs2[1]);
                            } else if (Float.valueOf(enviromentalItem.getMax()) < Float.valueOf(vs2[1])) {
                                enviromentalItem.setMax(vs2[1]);
                            }
                            if (StrUtil.isEmpty(enviromentalItem.getMin())) {
                                enviromentalItem.setMin(vs2[1]);
                            } else if (Float.valueOf(enviromentalItem.getMin()) > Float.valueOf(vs2[1])) {
                                enviromentalItem.setMin(vs2[1]);
                            }
                            break;
                        }
                    }
                }

            }
            //Rain:2.2;temperatue:5.17;PGV:2.5;Pressure:91.27;humity:68.05;lamination:58318;PGA:5.1;
        }

        AppletMonitorDataDto dto = new AppletMonitorDataDto();

        // 查找风险参数
        // 获取灾害指数
        Monitordata monitordataLast = monitordataMapper.findLastByCode(param.getProjectCode());
        if(monitordataLast != null){
            if(monitordataLast.getAlarmval().equals("low"))
                monitordataLast.setAlarmval("低风险");
            else if(monitordataLast.getAlarmval().equals("middle_low"))
                monitordataLast.setAlarmval("中低风险");
            else if(monitordataLast.getAlarmval().equals("middle"))
                monitordataLast.setAlarmval("中风险");
            else if(monitordataLast.getAlarmval().equals("high"))
                monitordataLast.setAlarmval("高风险");
        }
        String disasterExponential = monitordataLast != null ? monitordataLast.getAlarmval() : "--";
        // 获取本周高风险天数
        Calendar calendar2 = Calendar.getInstance();
        String endTime = DateUtil.formatDateTime(calendar2.getTime());
        calendar2.set(Calendar.DAY_OF_WEEK, 1);
        String startTime = DateUtil.formatDateTime(calendar2.getTime());
        Integer days = monitordataMapper.findHighAlarmvalDaysThisWeek(param.getProjectCode(), startTime, endTime);

        dto.setDisasterExponential(disasterExponential);
        dto.setDate(DateUtil.format(DateUtil.parse(monitordataLast.getDate().replaceAll("T", " ")), "MM月dd日"));
        dto.setHighDisasterExponentialDays(days);
        dto.setEnviromentalList(enviromentalListItemList);
        dto.setKdigList(kdigListItemList);
        dto.setVvigList(vvigListItemList);
        dto.setTopParamsList(topParamsItemDtoList);
        return new ResponseResult<AppletMonitorDataDto>().success().setData(dto);
    }

    public ResponseResult addTopParam(AppletMonitorAddTopParam param) {
        // 验证token
        TokenUtil.verifyToken();
        // 验证项目权限
        User user = TokenUtil.verifyAuthority(param.getAccessToken(), null);

        TopParam topParam = topParamMapper.findOne(param.getParamName(), user.getAccount(), param.getProjectCode(), param.getType());
        if (topParam != null) {
            return new ResponseResult().error().message("请勿重复添加");
        }
        TokenUtil.verifyAuthority(param.getAccessToken(), null);
        topParam = new TopParam();
        topParam.setAccount(user.getAccount());
        topParam.setParamName(param.getParamName());
        topParam.setType(param.getType());
        topParam.setProjectCode(param.getProjectCode());
        topParamMapper.insertSelective(topParam);
        return new ResponseResult().success();
    }

    public ResponseResult delTopParam(AppletMonitorDeleteTopParam param) {
        // 验证token
        TokenUtil.verifyToken();
        // 验证项目权限
        User user = TokenUtil.verifyAuthority(param.getAccessToken(), null);
        topParamMapper.deleteByPrimaryKey(param.getId());
        return new ResponseResult().success();
    }

    public ResponseResult<AppletMonitorViewDto> view(AppletMonitorViewParam param) {
        // 验证token
        TokenUtil.verifyToken();

        // 验证项目权限
        TokenUtil.verifyAuthority(param.getAccessToken(), param.getProjectCode());

        AppletMonitorViewDto dto = new AppletMonitorViewDto();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate(param.getEndDate()));
        String fillEndDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
        Long fillEndTime = calendar.getTimeInMillis();

        calendar.setTime(DateUtil.parseDate(param.getStartDate()));
        String fillStartDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");

        List<String> fillXData = new ArrayList<>();
        while (calendar.getTimeInMillis() <= fillEndTime) {
            fillXData.add(DateUtil.format(calendar.getTime(), "MM.dd"));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        // 查找时间段内，该项目的数据
        List<Monitordata> monitordataList = monitordataMapper.findByCode(param.getProjectCode(), fillStartDate, fillEndDate);
        // 获取核心检测数据
        dto.setXData(fillXData);
        List<String> yList = new ArrayList<>();
        calendar.setTime(DateUtil.parseDate(fillStartDate));
        while (calendar.getTimeInMillis() <= fillEndTime) {
            Boolean has = false;
            for (Monitordata monitordata : monitordataList) {
                String d = DateUtil.formatDate(calendar.getTime());
                if (monitordata.getDate().contains(d)) {
                    has = true;

                    if (param.getType() == E_TOP_PARAM_TYPE.环境变量.getValue()) {
                        String overalldd = monitordata.getOveralldd();
                        String[] overalldds = overalldd.split(";");
                        for (String key : overalldds) {
                            String[] vs = key.split(":");
                            if (vs[0].equals(param.getParamName()))
                                yList.add(vs[1]);
                        }
                    } else if (param.getType() == E_TOP_PARAM_TYPE.波速变化均值.getValue()) {
                        String vvig = monitordata.getVvig();
                        String[] vvigs = vvig.split(";");
                        for (String kv : vvigs) {
                            //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                            String[] vs = kv.split(":");
                            String name = param.getParamName();
                            String[] names = name.split(":");
                            String[] v1s = vs[0].split("_");
                            String[] v2s = names[0].split("-");
                            if (v2s.length == 2 && (
                                    (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1]))
                            )) {
                                if (vs[1].compareTo("100") > 0)
                                    yList.add(null);
                                else
                                    yList.add(vs[1]);
                                break;
                            } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                                if (vs[1].compareTo("100") > 0)
                                    yList.add(null);
                                else
                                    yList.add(vs[1]);
                                break;
                            }
                        }
                    } else if (param.getType() == E_TOP_PARAM_TYPE.微裂隙变化均值.getValue()) {
                        String kdig = monitordata.getKdig();
                        String[] kdigs = kdig.split(";");
                        for (String kv : kdigs) {
                            //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                            String[] vs = kv.split(":");
                            String name = param.getParamName();
                            String[] names = name.split(":");
                            String[] v1s = vs[0].split("_");
                            String[] v2s = names[0].split("-");
                            if (v2s.length == 2 && (
                                    (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1]))
                            )) {
                                if (vs[1].compareTo("100") > 0)
                                    yList.add(null);
                                else
                                    yList.add(vs[1]);
                                break;
                            } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                                if (vs[1].compareTo("100") > 0)
                                    yList.add(null);
                                else
                                    yList.add(vs[1]);
                                break;
                            }
                        }
                    }
                }
            }
            if (!has) {
                yList.add(null);
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        dto.setYData(yList);
        return new ResponseResult().success().setData(dto);
    }
}
